package com.pc.tas.learning.trace;

import org.json.JSONException;
import org.json.JSONObject;

import java.sql.Timestamp;

public class Trace {

	
    protected String id = "";
    protected long pageId;
    protected String userAgent;
    protected String iP;
    protected int userId;
    protected Timestamp createdate;
    protected String actionInfo;
    protected String userName;
    protected String parameters;
    protected String referer;
    protected int type;
    protected String url;
    protected String payload;
    
    protected String clientId;

	public String getPayload() {
		return payload;
	}

	public void setPayload(String payload) {
		this.payload = payload;
	}

	public Trace() {    	
		super();		
		this.createdate = new Timestamp(System.currentTimeMillis());
	}
    
    public Trace(String json) throws JSONException {    	
    	JSONObject jitem = new JSONObject(json); 
    	try {
    		this.setActionInfo(jitem.getString("actionInfo"));
    	}catch(Exception e) {}    	
    	this.setId(jitem.getString("id"));
    	this.setIP(jitem.getString("ip"));    	
    	this.setPageId(jitem.getLong("pageId"));
    	try {
    		this.setParameters(jitem.getString("parameters"));
    	}catch(Exception e) {}
    	this.setReferer(jitem.getString("referer"));
    	this.setType(jitem.getInt("type"));    	
    	try {
    		this.setUrl(jitem.getString("url"));
    	}catch(Exception e){}
    	this.setUserAgent(jitem.getString("userAgent"));    	
    	try {
    		this.setUserId(jitem.getInt("userId"));
        	this.setUserName(jitem.getString("userName"));
    	}catch(Exception e) {}
    	this.setPayload(jitem.getString("payload"));
    	try{
    		this.setClientId(jitem.getString("clientId"));    	
    	}catch(Exception e) {}
    	
    	//时间  
    	this.setCreatedate(new Timestamp(jitem.getLong("createdate")));
    }

    public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

    public long getPageId() {
        return pageId;
    }

    public void setPageId(long pageId) {
        this.pageId = pageId;
    }
    
    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }
    public String getIP() {
        return iP;
    }

    public void setIP(String iP) {
        this.iP = iP;
    }
    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
    public Timestamp getCreatedate() {
        return createdate;
    }

    public void setCreatedate(Timestamp createdate) {
        this.createdate = createdate;
    }
    public String getActionInfo() {
        return actionInfo;
    }

    public void setActionInfo(String actionInfo) {
        this.actionInfo = actionInfo;
    }
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }
    public String getReferer() {
        return referer;
    }

    public void setReferer(String referer) {
        this.referer = referer;
    }
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    
    public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public boolean equals(Object obj) {
        if (this == obj)
            return true;

        if (obj == null || !(obj instanceof Trace))
            return false;

        Trace bean = (Trace) obj;
        if (bean.getId() !=this.getId())
            return false;

        return true;
    }
    
    public String toJSON() throws JSONException {
    	JSONObject jitem = new JSONObject();        
        jitem.put("id", this.getId());
        jitem.put("pageId", this.getPageId());
        jitem.put("userId", this.getUserId());
        jitem.put("userName", this.getUserName());
        jitem.put("type", this.getType());
        jitem.put("actionInfo", this.getActionInfo());
        jitem.put("createdate", this.getCreatedate().getTime());
        jitem.put("ip", this.getIP());
        jitem.put("url", this.getUrl());
        jitem.put("parameters", this.getParameters()==null?"":this.getParameters());
        jitem.put("referer", this.getReferer()==null? "" : this.getReferer());
        jitem.put("userAgent", this.getUserAgent()==null? "":this.getUserAgent());
        jitem.put("payload", this.getPayload() == null ? "" : this.getPayload());
        jitem.put("clientId", this.getClientId() == null ? "" : this.getClientId());
    	return jitem.toString();
    }    
}
